<?php
namespace Api\Controller;
use Think\Controller;
class LoginController extends Controller {
    //账号密码登录
	public function Login(){
        $utypearray=array('user','doctor');
		$phone     =I('phone');
		$password  =I('password');
		$utype     =I('utype');
		if(empty($password)){
			$returninfo='请填写密码!';
        	EchoData($uinfo,0,109,$returninfo,0);
		}
		if(!in_array($utype, $utypearray)){
			$returninfo='utype类型错误';
        	EchoData($uinfo,0,105,$returninfo,0);
		}
        if($utype=='user'){//用户登录
        	$uinfo=D('User')->findone($phone,md5($password));
        	$info='用户';
        }else{//医生登录
        	$uinfo=D('Doctor')->findone($phone,md5($password));
        	$info='医生';
        }
        //登录之后会从新更新token 发送给前端用来 请求验证
        if($uinfo){
        	EchoData($uinfo);
        }else{
        	$returninfo='账号或密码错误';
        	$uinfo='';
        	EchoData($uinfo,0,101,$returninfo,0);
        }
    }

    //小程序code登录
    public function codeLogin(){
        $utypearray=array('user','doctor');
        $utype      = I("utype");//user用户端 doctor医生端
        if(!in_array($utype, $utypearray)){
          EchoData('',0,101,'utype类型错误',0);
        }
        $code      = I("code");
        $appid     = I("appid");
        $appsecret = I("appsecret");
        $res = curl("https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$appsecret}&js_code={$code}&grant_type=authorization_code");
        $returninfo=json_decode($res);
        $openid    =$returninfo->openid;
        if($utype=='user'){
            $userinfo=D('User')->toOpenidGetUserInfo($openid);
            if($userinfo&&$userinfo['phone']){
             EchoData($userinfo);
            }else{
             EchoData($openid,0,110,'需设置手机号码',1);
            }
        }
    }
}